﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Model.cs" company="">
//   
// </copyright>
// <summary>
//   The model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------



using System;

namespace cz.burger.windows.MDCCTaxi
{
    using System.Collections.Generic;
    using System.Linq;

    using global::MDCCTaxi;
    using System.Threading.Tasks;
    using System.ComponentModel;
    using System.Windows.Media;

    /// <summary>
    /// The model.
    /// </summary>
    public class Model: INotifyPropertyChanged
    {

        public Model()
        {
            this.Result = "Initializing...";
        }

        public bool Logged { get; private set; }

        /// <summary>
        /// Gets the result.
        /// </summary>
        public string Result
        {
            get { return _result; }
            private set
            {
                _result = value;
                OnPropertyChanged("Result");
            }
        }

        public List<RideData> Rides { get; private set; }

        /// <summary>
        /// The proxy.
        /// </summary>
        //private IWebProxy proxy = new LocalProxy();
        private IWebProxy proxy = new WebProxy();

        /// <summary>
        /// The login.
        /// </summary>
        internal async Task Login()
        {
            var data = await this.proxy.Login();
            this.ContinueLogged(data);
        }

        internal void Reset()
        {
            this.Result = "Reloading...";
            this.Rides.Clear();
            this.OnPropertyChanged("Rides");
        }

        private void ContinueLogged(WebProxyData data)
        {
            this.Logged = data.Logged;
            this.Result = data.Logged ? "Logged in" : "Error: " + data.ErrorMessage;
        }

        internal async Task LoadStatus() {
            var data = await this.proxy.LoadStatus();
            this.ShowStatus(data);
        }

        private Dictionary<int, Color> ColorMap = new Dictionary<int, Color> 
        { 
            { 1, Colors.Red },    // 1 = not sent yet (red)
            { 2, Colors.Orange }, // 2 = taxi on its way (yellow)
            { 3, Colors.Green },  // 3 = with customer (green)
            { 97, Colors.Gray },  // 97 = canceled (purple, crossed)
            { 98, Colors.Gray },  // 98 = trip reported as no-show (purple)
            { 99, Colors.Gray }   // 99 = trip completed (black)
        };

        private Color CodeToColor(int code)
        {
            return this.ColorMap[code];
        }

        private void ShowStatus(WebProxyData data)
        {
            var resultMsg = data.Rides.Count == 0 ? "No rides found ({0})" : "Rides loaded ({0})";
            resultMsg = String.Format(resultMsg, DateTime.Now.ToShortTimeString());
            this.Result = resultMsg + "\nTap here or shake for refresh...";
            this.Rides = data.Rides;
            this.Rides.ForEach(r =>
            {
                r.Text = String.Format("{0}\n{1}", r.Time, r.Title);
                r.Background = new SolidColorBrush(CodeToColor(r.Code)); 
            });
            this.OnPropertyChanged("Rides");
        }

        /// <summary>
        /// The _result.
        /// </summary>
        private string _result;

        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
